package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.lab.LabFeeitemVsItemDictMapper;
import com.ysd.lis.mapper.lab.LabReqAdditionsMapper;
import com.ysd.lis.mapper.lab.LabReqMainMapper;
import com.ysd.lis.mapper.lab.LabReqdetailMapper;
import com.ysd.lis.service.lab.LabReqdetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 申请明细表 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2023-12-26
 */
@Service
public class LabReqdetailServiceImpl extends ServiceImpl<LabReqdetailMapper, LabReqdetail> implements LabReqdetailService {

    @Autowired
    LabReqdetailMapper labReqdetailMapper;
    @Autowired
    private LabReqMainMapper labReqMainMapper;
    @Autowired
    LabReqAdditionsMapper labReqAdditionsMapper;
    @Autowired
    LabFeeitemVsItemDictMapper labFeeitemVsItemDictMapper;

    @Override
    public Result getDetailByBarcode(String reqNo) {
        if (StringUtils.isEmpty(reqNo)) {
            return Result.succ(1, "申请编号为空", "");
        }
        LambdaQueryWrapper<LabReqdetail> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(LabReqdetail::getDelFlag, 0);
        if (!StringUtils.isEmpty(reqNo)) {
            queryWrapper.eq(LabReqdetail::getReqNo, reqNo);
        }
        List<LabReqdetail> labReqdetailList = labReqdetailMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功!", labReqdetailList);

    }


    @Override
    public Result findReqDetailByReqNo(String reqNo) {
        if (StringUtils.isBlank(reqNo)) {
            return Result.succ(1, "查询成功", new ArrayList<>());
        }
        List<LabReqdetail> additionLabReqdetails = new ArrayList<>();
        MPJLambdaWrapper<LabReqdetail> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabReqdetail.class);
        queryWrapper.selectAs("fee", LabFeeItem::getId, LabReqdetail::getFeeItemId);
        queryWrapper.selectAs("fee", LabFeeItem::getInputCode, LabReqdetail::getInputCode);
        queryWrapper.selectAs("fee", LabFeeItem::getPrice, LabReqdetail::getPrice);
        queryWrapper.selectAs("lhr", LabHisReqQuery::getAdviceId, LabReqdetail::getAdviceId);
        queryWrapper.leftJoin(LabFeeItem.class, "fee", p -> p.eq(LabFeeItem::getFeeCode, LabReqdetail::getFeeCode).eq(LabFeeItem::getDelFlag, 0));
        queryWrapper.leftJoin(LabHisReqQuery.class, "lhr", p -> p.eq(LabHisReqQuery::getReqNo, LabReqdetail::getReqNo).eq(LabHisReqQuery::getFeeCode, LabReqdetail::getFeeCode).eq(LabHisReqQuery::getDelFlag, 0));
        queryWrapper.eq(StringUtils.isNotBlank(reqNo), LabReqdetail::getReqNo, reqNo);
        List<LabReqdetail> labReqdetails = labReqdetailMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(labReqdetails)) {
            labReqdetails.forEach(item -> item.setIsAddition(0));
        }
        //获取附加条码产生的费用
        MPJLambdaWrapper<LabReqAdditions> reqAddititonWrapepr = new MPJLambdaWrapper<>();
        reqAddititonWrapepr.selectAll(LabReqAdditions.class)
                .eq(LabReqAdditions::getDelFlag, 0)
                .leftJoin(LabReqMainQuery.class, LabReqMainQuery::getId, LabReqAdditions::getReqId)
                .eq(LabReqMainQuery::getReqNo, reqNo);
        List<LabReqAdditions> labReqAdditions = labReqAdditionsMapper.selectList(reqAddititonWrapepr);
        List<String> reqAdditionIds = labReqAdditions.stream().map(LabReqAdditions::getAdditionReqId).distinct().collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(reqAdditionIds)) {
            List<String> reqMainReqNos = labReqMainMapper.selectBatchIds(reqAdditionIds).stream().map(LabReqMain::getReqNo).distinct().collect(Collectors.toList());
            MPJLambdaWrapper<LabReqdetail> additionWrapper = new MPJLambdaWrapper<>();
            queryWrapper.selectAs("fee", LabFeeItem::getId, LabReqdetail::getFeeItemId);
            queryWrapper.selectAs("fee", LabFeeItem::getInputCode, LabReqdetail::getInputCode);
            queryWrapper.selectAs("fee", LabFeeItem::getPrice, LabReqdetail::getPrice);
            queryWrapper.selectAs("lhr", LabHisReqQuery::getAdviceId, LabReqdetail::getAdviceId);
            queryWrapper.leftJoin(LabFeeItem.class, "fee", p -> p.eq(LabFeeItem::getFeeCode, LabReqdetail::getFeeCode).eq(LabItemDict::getDelFlag, 0));
            queryWrapper.leftJoin(LabHisReqQuery.class, "lhr", p -> p.eq(LabHisReqQuery::getReqNo, LabReqdetail::getReqNo).eq(LabHisReqQuery::getFeeCode, LabReqdetail::getFeeCode).eq(LabHisReqQuery::getDelFlag, 0));
            additionWrapper.in(LabReqdetail::getReqNo, reqMainReqNos);
            additionLabReqdetails = labReqdetailMapper.selectList(additionWrapper);
            if (CollectionUtils.isNotEmpty(additionLabReqdetails)) {
                additionLabReqdetails.forEach(item -> item.setIsAddition(1));
            }
        }
        labReqdetails.addAll(additionLabReqdetails);
        List<LabReqdetail> collect = labReqdetails.stream().sorted(Comparator.comparingInt(LabReqdetail::getIsAddition)).collect(Collectors.toList());
        return Result.succ(1, "查询成功", collect);
    }

    @Override
    public Result findReqDetailTotalCountByReqNo(String reqNo, String rptGroup) {
        LambdaQueryWrapper<LabReqdetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(reqNo), LabReqdetail::getReqNo, reqNo);
        List<LabReqdetail> labReqdetails = labReqdetailMapper.selectList(queryWrapper);

        if (CollectionUtils.isEmpty(labReqdetails)) {
            return Result.succ(1, "查询成功", 0);
        }
        //查询出关联所有的申请项目数量
        List<String> feeItems = labReqdetails.stream().map(LabReqdetail::getFeeCode).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(feeItems)) {
            MPJLambdaWrapper<LabFeeitemVsItemDict> mpjLambdaWrapper = new MPJLambdaWrapper<>();
            mpjLambdaWrapper.select(LabFeeitemVsItemDict::getItemNo).distinct().in(LabFeeitemVsItemDict::getFeeCode, feeItems)
                    .eq(LabFeeitemVsItemDict::getDelFlag, 0)
                    .eq(LabFeeitemVsItemDict::getRptGroup, rptGroup)
                    .innerJoin(LabItemDict.class, "dic", p -> p.eq(LabItemDict::getDelFlag, 0).eq(LabItemDict::getItemNo, LabFeeitemVsItemDict::getItemNo).eq(LabItemDict::getRptGroup, LabFeeitemVsItemDict::getRptGroup));
            List<LabFeeitemVsItemDict> labFeeitemVsItemDicts = labFeeitemVsItemDictMapper.selectList(mpjLambdaWrapper);
            return Result.succ(1, "查询成功", labFeeitemVsItemDicts.size());
        }
        return Result.succ(1, "查询成功", 0);
    }
}
